home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / language / harvest.cpt / Harvest C / DTokenStream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-16  |  4.0 KB  |  211 lines

  1. /*
  2.     Harvest C
  3.     Copyright 1992 Eric W. Sink.  All rights reserved.
  4.     
  5.     This file is part of Harvest C.
  6.     
  7.     Harvest C is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU Generic Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.     
  12.     Harvest C is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     
  17.     You should have received a copy of the GNU General Public License
  18.     along with Harvest C; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     
  21.     Harvest C is not in any way a product of the Free Software Foundation.
  22.     Harvest C is not GNU software.
  23.     Harvest C is not public domain.
  24.  
  25.     This file may have other copyrights which are applicable as well.
  26.  
  27. */
  28.  
  29. /*
  30.         DTokenStream.c
  31.  
  32. */
  33.  
  34. #include "DTokenStream.h"
  35.  
  36. void DTokenStream::ITokenStream(DHashTable *defines)
  37. {
  38.     theDefines = defines;
  39.     theTokens = new CList;
  40.     theTokens->IList();
  41.     collectorStack = new CStack;
  42.     collectorStack->IStack();
  43.     topCollector = NULL;
  44.     parenLevel = 0;
  45. }
  46.  
  47. long DTokenStream::GetNumItems(void)
  48. {
  49.     return theTokens->GetNumItems();
  50. }
  51.  
  52. DToken *DTokenStream::GetNthToken(long n)
  53. {
  54.     return (DToken *) theTokens->NthItem(n);
  55. }
  56.  
  57. void DTokenStream::PushCollector(void)
  58. {
  59.     collectorStack->Push((void *) topCollector);
  60. }
  61.  
  62. void DTokenStream::StartCollecting(DMacroCollector *aCollector)
  63. {
  64.     topCollector = aCollector;
  65.     topCollector->startingParenLevel = parenLevel;
  66. }
  67.  
  68. void DTokenStream::RawAdd(DToken *theToken) 
  69. {
  70.     theTokens->Append((void *) theToken);
  71. }
  72.  
  73. Boolean DTokenStream::IsEmpty(void)
  74. {
  75.     return (theTokens->IsEmpty());
  76. }
  77.  
  78. Boolean DTokenStream::Add(DToken *theToken,CSymbolList *beingExpanded)
  79. {
  80.     DDefine *ppSymbol;
  81.     
  82.     if (theToken->IsExpandable()) 
  83.     {
  84.         if (theDefines) 
  85.         {
  86.             if (ppSymbol = (DDefine *) theDefines->Find(theToken->name)) 
  87.             {
  88.                 /* We've found a symbol with a preproc definition.  We first
  89.                     check to see if it is already being expanded */
  90.                 if (beingExpanded) 
  91.                 {
  92.                     if (beingExpanded->Find(theToken->name)) 
  93.                     {
  94.                         /* Found it - no recursive expansion */
  95.                         if (topCollector) 
  96.                         {
  97.                             topCollector->AddToArg(theToken);
  98.                         }
  99.                         else 
  100.                         {
  101.                             RawAdd(theToken);    
  102.                         }
  103.                     }
  104.                     else 
  105.                     {
  106.                         ppSymbol->Expand(this,beingExpanded);
  107.                     }
  108.                 }
  109.                 else 
  110.                 {
  111.                     ppSymbol->Expand(this,beingExpanded);
  112.                 }
  113.             }
  114.             else 
  115.             {
  116.                 if (topCollector) 
  117.                 {
  118.                     topCollector->AddToArg(theToken);
  119.                 }
  120.                 else 
  121.                 {
  122.                     RawAdd(theToken);    
  123.                 }
  124.             }
  125.         }
  126.         else 
  127.         {
  128.             /* Then this is not an expanding stream */
  129.             RawAdd(theToken);    
  130.         }
  131.     }
  132.     else if (theToken->code == '(')
  133.     {
  134.         parenLevel++;
  135.         if (topCollector) 
  136.         {
  137.             if (parenLevel == (topCollector->startingParenLevel + 1)) 
  138.             {
  139.                 /* Then we have just started fetching args */
  140.                 topCollector->StartNextArg();
  141.             }
  142.             else 
  143.             {
  144.                 topCollector->AddToArg(theToken);
  145.             }
  146.         }
  147.         else 
  148.         {
  149.             RawAdd(theToken);    
  150.         }
  151.     }
  152.     else if (theToken->code == ',')
  153.     {
  154.         if (topCollector) 
  155.         {            
  156.             if (parenLevel == (topCollector->startingParenLevel + 1)) 
  157.             {
  158.                 /* Then we have just completed fetching one arg */
  159.                 topCollector->StartNextArg();
  160.             }
  161.             else 
  162.             {
  163.                 topCollector->AddToArg(theToken);
  164.             }
  165.         }
  166.         else 
  167.         {
  168.             RawAdd(theToken);    
  169.         }
  170.     }
  171.     else if (theToken->code == ')') 
  172.     {
  173.         parenLevel--;
  174.         if (topCollector) 
  175.         {
  176.             if (parenLevel == (topCollector->startingParenLevel)) 
  177.             {
  178.                 /* Then we have just completed fetching all the args */
  179.                 topCollector->Expand(this,beingExpanded);
  180.             }
  181.             else 
  182.             {
  183.                 topCollector->AddToArg(theToken);
  184.             }
  185.         }
  186.         else 
  187.         {
  188.             RawAdd(theToken);    
  189.         }
  190.     }
  191.     else 
  192.     {
  193.         if (topCollector) 
  194.         {
  195.             topCollector->AddToArg(theToken);
  196.         }
  197.         else 
  198.         {
  199.             RawAdd(theToken);    
  200.         }
  201.     }
  202. }
  203.  
  204. void DTokenStream::Dispose(void)
  205. {
  206.     theTokens->Dispose();
  207.     collectorStack->Dispose();
  208.     inherited::Dispose();
  209. }
  210.  
  211.